/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package MongoDAL;

import Acquaintance.IAHouse;
import Acquaintance.IAUser;
import DALFactory.IDataMapper;
import Entity.EHouse;
import Entity.EUser;
import com.mongodb.BasicDBObject;
import com.mongodb.DBCollection;
import com.mongodb.DBCursor;
import com.mongodb.DBObject;
import java.util.HashMap;

/**
 *  MongoDB Data实体  实现了IDataMapper接口
 * @author kocio
 */
public class MongoDataMapper implements IDataMapper {

    private MongoStatement st = null;
    private HashMap<String,IAHouse> HouseMap = null;
    private HashMap<String,IAUser> UserMap = null;

    /**
     *
     */
    public MongoDataMapper() {
        this.HouseMap = new HashMap<String,IAHouse>();
        this.UserMap = new HashMap<String,IAUser> ();
    }

    /**
     *
     * @return
     */
    public EHouse getHouse() {
        st = new MongoStatement();
        DBCollection colls = st.QueryTable("House");

        EHouse eh = null;
        eh = (EHouse) mapHouse(colls);
        st=null;
        return eh;
    }

    /**
     *
     * @param coll
     * @return
     */
    private IAHouse mapHouse(DBCollection coll) {
        IAHouse eh = null;
        DBCursor cur = coll.find();
        while (cur.hasNext()) {
            eh = CreateHouse();
            DBObject myDoc = cur.next();

            int HouserPrice = Integer.parseInt(myDoc.get("Price").toString().trim());
            int Floor = Integer.parseInt(myDoc.get("Floor").toString().trim());
            String Location = myDoc.get("Location").toString().trim();
            String Premise = myDoc.get("Premise").toString().trim();
            String Type = myDoc.get("Type").toString().trim();
            String USERNAME = " ";
            String Arrow = myDoc.get("Arrow").toString().trim();
            float Area = Float.parseFloat(myDoc.get("Area").toString().trim());

            if (myDoc.get("USERNAME") != null) {
                USERNAME = myDoc.get("USERNAME").toString().trim();
            }
            int HID = (Integer) myDoc.get("_id");
            eh.setHID(HID);
            eh.setHouseHoster(USERNAME);
            eh.setHousePrice(HouserPrice);
            eh.setObjectID("HID" + HID);
            eh.setFloor(Floor);
            eh.setLocation(Location);
            eh.setPremise(Premise);
            eh.setType(Type);
            eh.setArea(Area);
            eh.setArrow(Arrow);
            HouseMap.put(eh.getObjectID(), eh);
        }
        return eh;
    }

    /**
     *
     * @return
     */
    private IAHouse CreateHouse() {
        return new EHouse();
    }

    /**
     *
     * @param ob
     * @return
     */
    public EUser getUser(DBObject ob) {
        EUser u = null;
        u = (EUser) mapUser(ob);

        return u;
    }

    /**
     *
     * @param myDoc
     * @return
     */
    private IAUser mapUser(DBObject myDoc) {
        IAUser u = null;
        u = CreateUser();
        float UID = Float.parseFloat(myDoc.get("UID").toString().trim());
        int uID = (int) UID;
        String USERNAME = myDoc.get("USERNAME").toString();
        String Email = " ";
        if (myDoc.get("E-mail") != null) {
            Email = myDoc.get("E-mail").toString();
        }
        u.setEmail(Email);
        u.setUserID(uID);
        u.setUserName(USERNAME);
        u.setObjectID(USERNAME);
        UserMap.put(u.getObjectID(), u);
        return u;
    }

    /**
     *
     * @return
     */
    private IAUser CreateUser() {
        return new EUser();
    }

    /**
     *
     * @return
     */
    public HashMap<String,IAHouse>  getHouseMap() {
        getHouse();
        return HouseMap;
    }

    /**
     *
     * @return
     */
    public HashMap<String,IAUser>  getUserMap() {
        st = new MongoStatement();
        DBCollection colls = st.QueryTable("User");
        IAUser user = null;
        DBCursor cur = colls.find();
        while (cur.hasNext()) {
            DBObject myDoc = cur.next();
            user = mapUser(myDoc);
        }
        return UserMap;
    }

    /**
     *
     * @param HID
     * @return
     */
    public EHouse getHouseByHID(String HID) {
        return new EHouse();
    }

    /**
     *
     * @param UserName
     * @return
     */
    public EUser getUserByUserName(String UserName) {
        return new EUser();
    }

    /**
     *
     * @param user
     */
    public void updateUser(EUser user) {
        DBObject updateCondition = new BasicDBObject();
        updateCondition.put("USERNAME", user.getUserName());

        DBObject updateValue = new BasicDBObject();
        updateValue.put("E-mail", user.getUserEmail());
        updateValue.put("PASSWD", user.getPassWD());
        DBObject updateSetValue = new BasicDBObject("$set", updateValue);

        st = new MongoStatement();
        DBCollection colls = st.QueryTable("User");
        colls.update(updateCondition, updateSetValue);

    }

    /**
     *
     * @param user
     */
    public int insertUser(EUser user) {
        DBObject newUser = new BasicDBObject();
        newUser.put("USERNAME", user.getUserName());
        newUser.put("PASSWD", user.getPassWD());
        newUser.put("E-mail", user.getUserEmail());

        st = new MongoStatement();
        DBCollection colls = st.QueryTable("User");
        DBCursor cur = colls.find().sort(new BasicDBObject("UID", -1)).limit(1);
        DBObject UUID = (DBObject) cur.next();
        System.out.println(UUID);
        float UID = Float.parseFloat(UUID.get("UID").toString());
        int uid = (int) UID;
        uid++;
        newUser.put("UID", uid);
        colls.insert(newUser);
        return uid;
    }

    /**
     *
     * @param user
     */
    public void deleteUser(EUser user) {
        DBObject delete = new BasicDBObject();
        delete.put("UserName", user.getUserName());
        st = new MongoStatement();
        DBCollection colls = st.QueryTable("User");
        colls.remove(delete);
    }

    /**
     *
     * @param house
     */
    public void updateHouse(EHouse house) {

        DBObject updateCondition = new BasicDBObject();
        updateCondition.put("HID", house.getHID());

        DBObject updateValue = new BasicDBObject();
        updateValue.put("USERNAME", house.getHouseHoster());
        updateValue.put("Price", house.getHousePrice());
        updateValue.put("Area", house.getArea());
        updateValue.put("Arrow", house.getArrow());
        updateValue.put("Premise", house.getPremise());
        updateValue.put("Type", house.getType());
        updateValue.put("Location", house.getLocation());
        updateValue.put("Floor", house.getFloor());
        DBObject updateSetValue = new BasicDBObject("$set", updateValue);

        st = new MongoStatement();
        DBCollection colls = st.QueryTable("House");
        colls.update(updateCondition, updateSetValue);
    }

    /**
     *
     * @param house
     */
    public void insertHouse(EHouse house) {
        DBObject newHouse = new BasicDBObject();
        newHouse.put("HID", house.getHID());
        newHouse.put("USERNAME", house.getHouseHoster());
        newHouse.put("Price", house.getHousePrice());
        newHouse.put("Area", house.getArea());
        newHouse.put("Arrow", house.getArrow());
        newHouse.put("Premise", house.getPremise());
        newHouse.put("Type", house.getType());
        newHouse.put("Location", house.getLocation());
        newHouse.put("Floor", house.getFloor());

        st = new MongoStatement();
        DBCollection colls = st.QueryTable("House");
        colls.insert(newHouse);
    }

    /**
     *
     * @param house
     */
    public void deleteHouse(EHouse house) {

        DBObject delete = new BasicDBObject();
        delete.put("HID", house.getHID());

        st = new MongoStatement();
        DBCollection colls = st.QueryTable("House");
        colls.remove(delete);
    }
}
